package com.transaction.manage.localMapper;

import com.transaction.manage.model.dto.TransApply;
import com.transaction.manage.myMapper.MyMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public interface TransApplyMapper extends MyMapper<TransApply> {

    void insertTransApply(@Param("channelCode") String channelCode,
                            @Param("handleDate") String handleDate,
                            @Param("flowStep") String flowStep,
                            @Param("dataList") List<Map<String, Object>> dataList);

    List<LinkedHashMap<String, Object>> selectTransApplyForMap(@Param("channelCode") String channelCode,
                                                               @Param("handleDate") String handleDate);

    List<LinkedHashMap<String, Object>> fundIsNotForChannelList(@Param("channelCode") String channelCode,
                                                                @Param("handleDate") String handleDate,
                                                                @Param("stepCode") Integer stepCode);

    List<LinkedHashMap<String, Object>> checkFundIsNotMappingToTaList(@Param("channelCode") String channelCode,
                                                                      @Param("handleDate") String handleDate,
                                                                      @Param("stepCode") Integer stepCode);

    List<LinkedHashMap<String, Object>> checkTransAccountIsNotValidList(@Param("channelCode") String channelCode,
                                                                        @Param("handleDate") String handleDate,
                                                                        @Param("stepCode") Integer stepCode);

    @Update("UPDATE trans_apply t SET t.ta_fund_code = ( SELECT r.ta_fund_code FROM fund_relation r WHERE t.channel_code = r.channel_code AND t.fund_code = r.fund_code AND r.valid_flag != '00' AND r.check_flag = '01' LIMIT 1 ) " +
            ", ta_account_id = ( SELECT ta_account_id FROM account_status s WHERE s.channel_code = t.channel_code AND s.transaction_account_id = t.transaction_account_id ) " +
            "WHERE t.channel_code = #{channelCode} AND t.handle_date = #{handleDate} ")
    void randomAllocationTaFundCode(@Param("channelCode") String channelCode,
                                    @Param("handleDate") String handleDate);

    @Select("SELECT count(*) FROM trans_apply t WHERE channel_code = #{channelCode} AND t.handle_date = #{handleDate} AND t.handle_date = #{fundCode} " +
            "AND t.transaction_account_id = #{transactionAccountId} " +
            "AND t.business_code = #{businessCode} AND t.application_amount > #{applyAmount} AND check_status = 1")
    int selectManyBuyTransSecondCheck(@Param("channelCode") String channelCode,
                                      @Param("handleDate") String handleDate,
                                      @Param("fundCode") String fundCode,
                                      @Param("transactionAccountId") String transactionAccountId,
                                      @Param("businessCode") String businessCode,
                                      @Param("applyAmount") BigDecimal applyAmount);

    @Select("SELECT IFNULL(sum(t.application_vol), 0) FROM trans_apply t WHERE channel_code = #{channelCode} AND t.handle_date = #{handleDate} AND t.handle_date = #{fundCode} " +
            "AND t.transaction_account_id = #{transactionAccountId} " +
            "AND t.business_code = '024' AND t.check_status = 1")
    BigDecimal selectManyRedeemTransSumVol(@Param("channelCode") String channelCode,
                                         @Param("handleDate") String handleDate,
                                         @Param("fundCode") String fundCode,
                                         @Param("transactionAccountId") String transactionAccountId,
                                         @Param("remainShares") BigDecimal remainShares);

    List<LinkedHashMap<String, Object>> selectTransApplySendTaForMap(@Param("channelCode") String channelCode,
                                                                     @Param("handleDate") String handleDate);

    void updateContractAndRequestNo(@Param("trans") LinkedHashMap<String, Object> trans,
                                    @Param("retMap") Map<String, Object> retMap);

    void updateTransSendTaStatusAndMsg(@Param("trans") LinkedHashMap<String, Object> trans,
                                       @Param("msg") String msg);

    List<LinkedHashMap<String, Object>> selectTrans020ForCfm(@Param("channelCode") String channelCode,
                                                             @Param("lastDay") String lastDay,
                                                             @Param("handleDate") String handleDate);

    List<LinkedHashMap<String, Object>> selectTransErrorForCfm(@Param("channelCode") String channelCode,
                                                               @Param("lastDay") String lastDay,
                                                               @Param("handleDate") String handleDate);
}